import {
  _decorator,
  Component,
  instantiate,
  Prefab,
  UITransform,
  view,
} from "cc";
import { random } from "./helper/utils";
const { ccclass, property } = _decorator;

@ccclass("AirPlaneManager")
export class AirPlaneManager extends Component {
  private _dW = view.getDesignResolutionSize().width;

  private _dh = view.getDesignResolutionSize().height;

  @property(Prefab)
  airPlanePrefab: Prefab = null;

  protected start(): void {
    // 生产敌机
    this.schedule(this._genAir, 0.5);
  }

  _genAir() {
    const airPlanePrefab = instantiate(this.airPlanePrefab);
    this.node.addChild(airPlanePrefab);

    airPlanePrefab.setPosition(
      this._randomX(airPlanePrefab.getComponent(UITransform)),
      this._dh
    );
  }

  private _randomX(uiTransform: UITransform) {
    const uiW = uiTransform.contentSize.width;
    const min = uiW + 50;
    const max = this._dW - uiW;
    return Math.floor(random(min, max));
  }
}
